package ReSofting.it.GoldDirector.Helpers;


	import java.lang.reflect.Type;
	import java.net.SocketException;
	import java.util.ArrayList;
	import java.util.List;
	import java.util.UUID;

	import org.apache.http.HttpEntity;
	import org.apache.http.HttpHost;
	import org.apache.http.HttpRequest;
	import org.apache.http.HttpResponse;
	import org.apache.http.client.HttpClient;
	import org.apache.http.client.methods.HttpGet;
	import org.apache.http.client.methods.HttpUriRequest;
	import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
	import org.apache.http.util.EntityUtils;
	import org.json.JSONArray;
	import org.json.JSONObject;

import ReSofting.it.GoldDirector.Interfaces.IRemoteCallCategorie;
	import ReSofting.it.GoldDirector.Interfaces.IRemoteCallListener;
import ReSofting.it.GoldDirector.JsonTrans.JsonTransformer_myCategoria;
import ReSofting.it.GoldDirector.Model.ReturnData;
import ReSofting.it.GoldDirector.Model.myCategoria;
import android.content.Context;
	import android.net.Uri;
	import android.os.AsyncTask;
import android.util.Log;



	public class GetCategoriesAsync extends AsyncTask<String, Void,  ReturnData< List<myCategoria>>> {
		 
	    private final static String TAG = GetDataAsync.class.getName();
	    
	    IRemoteCallCategorie<ReturnData< List<myCategoria>>>  listener;
	    Context _context= null;
	    ReturnData< List<myCategoria>>  _retData = null ;
	    
	    public GetCategoriesAsync(IRemoteCallCategorie<ReturnData< List<myCategoria>>>  listener, Context pContext) {
	        this.listener = listener;
	        _context = pContext;
	    }
	 
	    @Override
	    protected ReturnData< List<myCategoria>> doInBackground(String... urls) 
	    {
	    	HttpEntity entity = null;
	        List<myCategoria> oggettiList = new ArrayList<myCategoria>();
	        String url = urls[0];
	        String _responseString = null;
	        HttpResponse _httpResponse = null;
	                
	        
	        //HttpHost host = new HttpHost("10.0.2.2" , 8096);
	        //HttpGet get = new HttpGet( "/Services/OrdersSvc.svc/GetCategorie" ); //url
	               
	        HttpParams httpParameters = new BasicHttpParams();
		     HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
		     HttpConnectionParams.setSoTimeout(httpParameters, 30000);
	               

	        DefaultHttpClient httpClient = new DefaultHttpClient();
	        httpClient.setParams(httpParameters);

	        String url2 = "http://192.168.1.2:8096/Services/OrdersSvc.svc/GetCategorie";
	        
	        HttpGet request = new HttpGet(url); //8096	 
	        request.setHeader("Accept", "application/json");
	        request.setHeader("Content-type", "application/json");
	
	        try {
	        	
	        	
	             HttpResponse response = httpClient.execute(request);    
	             
	         	_httpResponse = httpClient.execute(request);   
	        	
	        	if(_httpResponse != null)
	             _responseString =   EntityUtils.toString(_httpResponse.getEntity());      
	             
	            if(_httpResponse == null | _responseString == null)
	            {
	            	 _retData = new ReturnData< List<myCategoria> >  (
	            			 9000, null,true,"response data is null", null);
	            	 
	            	 return _retData; //return
	            }
	            else 
	            {
	            	 _retData = new ReturnData< List<myCategoria> >  (
	            			 _httpResponse.getStatusLine().getStatusCode(),
	            			 _httpResponse.getAllHeaders(), false,"", oggettiList  );
	            }
	             
	            if(_responseString == null)
	            	return _retData;
	                                       
	             
	             //if(response.getStatusLine().getStatusCode() == 200)
	             //}
	                 String responseString = EntityUtils.toString(response.getEntity());
	                 JSONArray rawPersonaList = new JSONArray(responseString);
	                 
	                 JsonTransformer_myCategoria jsonner = new JsonTransformer_myCategoria();
	                 JSONObject jsObj = null;     
	                 myCategoria dato = null ;
	                 
	                 for(int i = 0; i < rawPersonaList.length(); i++) {

	                     jsObj = rawPersonaList.getJSONObject(i);	                     
	                     dato = jsonner.jsonTo_myCategoria(jsObj);	                	 
	                 	 oggettiList.add(dato);
	                 }
	            //}
	             
	             //oggettiList.add("\r\n SC:"+ response.getStatusLine());
	        }
	                 catch(SocketException ex)
	                 {
	                   Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
	                     ex.printStackTrace();
	                     
	                    oggettiList.add( new myCategoria( "Error",  "Error : "+ ex.getMessage()) );
	                    
	                    //resVarie.Alert2("Error in getCategorieAsync", ex.getMessage(), _context);
	                 }
	             
	         catch (Exception e) {
	            Log.d(TAG, e.getMessage());
	            
	            oggettiList.add(new myCategoria( "Error",  "Error : "+ e.getMessage()));
	            
	            e.printStackTrace();            
	            
	        }
	       
	        
	         return _retData;
	    }
	 
	    protected void onPostExecute(ReturnData< List<myCategoria>> retData) {
	       listener.onRemoteCallCategorieCompleted(retData);
	    }
	 
	}

